import { getappvm } from "./appvm";

import { resolvemdpathfromurl } from "./resolvemdpathfromurl";
import { states } from "./states";

export default async function csrloadpage() {
    const { pageerrors } = states;
    const appvm = getappvm();
    const path: string = resolvemdpathfromurl();

    // show_loading();
    try {
        if (!process.env.PRERENDER) {
            const { commonloadpage } = await import("./commonloadpage");
            await commonloadpage(states, path);
        }
        Reflect.set(appvm, "showerror", false);
        return;
    } catch (e_1) {
        console.error(e_1);
        Reflect.set(appvm, "errorcontent", "加载失败 " + path);

        // stop_loading();
        Reflect.set(appvm, "showerror", true);
        console.warn("load failed " + path);

        // if (process.env.PRERENDER) {
        //     if (Object.keys(pathrouter.getparams()).length !== 0) {
        //         requestAnimationFrame(() => {
        //             setTimeout(() => {
        //                 console.error(
        //                     "Opps! ... File not found!\n5秒后返回主页"
        //                 );
        //                 pregohome();
        //             }, 1000);
        //         });
        //     }
        // } else {
        if (location.hash !== "" && location.hash !== "#") {
            requestAnimationFrame(() => {
                setTimeout(() => {
                    console.error("Opps! ... File not found!\n5秒后返回主页");
                    location.hash = "";
                }, 1000);
            });
        }
        // }

        pageerrors.set(path, String(e_1));

        throw e_1;
    }
}
